package com.glodon.paas.account.api.oauth1;

import net.oauth.OAuthMessage;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import javax.xml.bind.annotation.XmlRootElement;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Access validation request message
 *
 * @author Don Li
 */
@XmlRootElement
public class AccessValidationRequest {
    private final Logger LOGGER = LoggerFactory.getLogger(this.getClass());

    private String method;
    private String url;
    private String oauthToken;
    private String consumerKey;
    private String oauthVersion = "1.0";
    private String oauthSignatureMethod;
    private String oauthSignature;
    private String oauthTimestamp;
    private String oauthNonce;
    private String authorization;
    private List<Parameter> params = new ArrayList<Parameter>();

    public AccessValidationRequest() {
    }


    public AccessValidationRequest(OAuthMessage message, HttpServletRequest request) {
        try {
            method = message.method;
            url = message.URL;
            oauthToken = message.getToken();
            consumerKey = message.getConsumerKey();
            oauthSignatureMethod = message.getSignatureMethod();
            oauthSignature = message.getSignature();
            oauthVersion = message.getParameter("oauth_version");
            oauthTimestamp = message.getParameter("oauth_timestamp");
            oauthNonce = message.getParameter("oauth_nonce");
            this.authorization = message.getAuthorizationHeader(null);


            // other params    
            for (Object e : request.getParameterMap().entrySet()) {
                Map.Entry<String, String[]> entry = (Map.Entry<String, String[]>) e;
                String name = entry.getKey();
                for (String value : entry.getValue()) {
                    if (StringUtils.startsWith(name, "oauth_")) {
                        continue;
                    }
                    params.add(new Parameter(name, value));
                }
            }

        } catch (IOException e) {
            LOGGER.debug("failed to build AccessValidationRequest", e);
        }
    }

    public String getMethod() {
        return method;
    }

    public void setMethod(String method) {
        this.method = method;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getOauthToken() {
        return oauthToken;
    }

    public void setOauthToken(String oauthToken) {
        this.oauthToken = oauthToken;
    }

    public String getConsumerKey() {
        return consumerKey;
    }

    public void setConsumerKey(String consumerKey) {
        this.consumerKey = consumerKey;
    }

    public String getOauthVersion() {
        return oauthVersion;
    }

    public void setOauthVersion(String oauthVersion) {
        this.oauthVersion = oauthVersion;
    }

    public String getOauthSignatureMethod() {
        return oauthSignatureMethod;
    }

    public void setOauthSignatureMethod(String oauthSignatureMethod) {
        this.oauthSignatureMethod = oauthSignatureMethod;
    }

    public String getOauthSignature() {
        return oauthSignature;
    }

    public void setOauthSignature(String oauthSignature) {
        this.oauthSignature = oauthSignature;
    }

    public String getOauthTimestamp() {
        return oauthTimestamp;
    }

    public void setOauthTimestamp(String oauthTimestamp) {
        this.oauthTimestamp = oauthTimestamp;
    }

    public String getOauthNonce() {
        return oauthNonce;
    }

    public void setOauthNonce(String oauthNonce) {
        this.oauthNonce = oauthNonce;
    }

    public String getAuthorization() {
        return authorization;
    }

    public void setAuthorization(String authorization) {
        this.authorization = authorization;
    }

    public List<Parameter> getParams() {
        return params;
    }

    public void setParams(List<Parameter> params) {
        this.params = params;
    }
}
